package leetcode.mess;

import java.util.Iterator;

/**
 * @author mazouri
 * @create 2021-10-05 22:26
 */
class PeekingIterator implements Iterator<Integer> {
    private Iterator<Integer> iterator;
    private Integer nextVal;

    public PeekingIterator(Iterator<Integer> iterator) {
        // initialize any member here.
        this.iterator = iterator;
        this.nextVal = iterator.next();
    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
        return nextVal;
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        Integer res = this.nextVal;
        nextVal = iterator.hasNext()?iterator.next():null;
        return res;
    }

    @Override
    public boolean hasNext() {
        return iterator.hasNext();
    }
}